<?php
class News {

    public function __construct() {
        $this->SQL = MySQL::getInstance();
    }

    public function add($post) {
        if(!isset($_SESSION['user']['id'])) {
            return FALSE;
        }
        return $this->SQL->fquery('
            INSERT INTO news
            (date, description, creator_id)
            VALUES
            ("%s", "%s", %d)',
            addslashes($post['date']),
            addslashes($post['description']),
            $_SESSION['user']['id']
        );
    }

    public function getAll() {
        return $this->SQL->fquery('
            SELECT n.date, n.description, COALESCE(u.name, "?") AS name
            FROM news n
            LEFT JOIN users u
                ON u.id = n.creator_id
            ORDER BY n.date DESC'
        );
    }
}
?>
